package de.wellenblau.objectGetter
{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Matrix;
	
	
	public class imgGetter extends objectGetter
	{
		private var _ldr:Loader;
		private var _width:uint;
		private var _height:uint;
		private var _keepProportion:Boolean;
		private var _fillColor:uint;
		private var _fillTransparent:Boolean = true;
		private var _overhead:Array;
		
		
		public function imgGetter(scope:Object, onComplete:Function, path:String,overhead:Array = null,keepProportion:Boolean=true,width:uint = 0,height:uint=0,fillColor:uint=0)
		{
			super(scope,onComplete,path);
			
			//config resizer
			_width = width;
			_height = height;
			_keepProportion = keepProportion;
			
			
			//config fillColor and transparents
			//config center
			_fillColor = fillColor;
			if(fillColor != 0){
				_fillTransparent = false;
			}
			
			//image overhead data 
			_overhead = overhead;
			
			
			//get the image
			_ldr = new Loader();
			_ldr.load(_myURL);
			_ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,onResponse);
			_ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgress);
			_ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError );
			
		}
		
		private function onError(e:IOErrorEvent):void
		{
			//callback([new Bitmap()]);
		}
		
		private function onResponse(e:Event):void
		{			
			var thumbMatrix:Matrix = new Matrix();
			var thumbScaleX:Number;
			var thumbScaleY:Number;
			var thumbDx:Number = 0;
			var thumbDy:Number = 0;
			
			//set result bitmap size
			if(_width != 0 && _height != 0){
			
			
				//keep proportion
				if(_keepProportion == true){
					if(_ldr.height >= _ldr.width){
						//scale
						thumbScaleX = thumbScaleY = _height / _ldr.height;
						//center
						thumbDx = (_width - _ldr.width * thumbScaleX) / 2;
						thumbDx /= thumbScaleX;
					}
					else{
						//scale
						thumbScaleX = thumbScaleY = _width / _ldr.width;
						//center
						thumbDy = (_height - _ldr.height * thumbScaleY) / 2;
						thumbDy /= thumbScaleX;
					}
					
					//config the move matrix
					thumbMatrix.translate(thumbDx,thumbDy);
				}
				//fill area
				else{
					//scaleX
					thumbScaleX = _width/_ldr.width;
					//scaleY
					thumbScaleY = _height/_ldr.height;
				}
				
				//config the scale matrix
				thumbMatrix.scale(thumbScaleX,thumbScaleY);
				
			}
			else{
				_width = _ldr.width;
				_height = _ldr.height;	
			}

			
			//generate thumb
			var thumbData:BitmapData = new BitmapData(_width,_height,_fillTransparent,_fillColor);
			thumbData.draw(_ldr,thumbMatrix);
			
			//create Bitmap
			var thumbBitmap:Bitmap = new Bitmap(thumbData);
			
			//send it back
			if(_overhead != null){ //with additional data
				callback([thumbBitmap,_overhead]);
			}
			else{ //without data	
				callback([thumbBitmap]);
			}
		} 
	}
}